local ascensionHud = nil
local teamCompleteNotify = nil
local checkpointThink = 0
local teamCompleteThink = 0

//Precache all the textures we're going to use.
local pointerOrange = surface.GetTextureID( "ascension/hud/map_pointer_orange" )
local pointerGreen = surface.GetTextureID( "ascension/hud/map_pointer_green" )
local pointerRed = surface.GetTextureID( "ascension/hud/map_pointer_red" )
local pointerBlue = surface.GetTextureID( "ascension/hud/map_pointer_blue" )

local countdownOne = surface.GetTextureID( "ascension/hud/countdown_1" )
local countdownTwo = surface.GetTextureID( "ascension/hud/countdown_2" )
local countdownThree = surface.GetTextureID( "ascension/hud/countdown_3" )
local countdownGo = surface.GetTextureID( "ascension/hud/countdown_go" )

local hudOne = surface.GetTextureID( "ascension/hud/icon_help" )
local hudTwo = surface.GetTextureID( "ascension/hud/icon_pickup" )
local hudThree = surface.GetTextureID( "ascension/hud/icon_overview" )

local backgroundOverlays = {}

local pointerBackground = surface.GetTextureID( "ascension/hud/map_neutral" )

local backgroundWidth = 128
local backgroundHeight = 512
local pointerSize = 64

local pointerXOffset = 20+(backgroundWidth*0.5)

local orangePos = 0
local greenPos = 0
local redPos = 0
local bluePos = 0

local timeToRestart = 0

local startMessage = false
local startTime = 0

function AscensionHUDInitialize()
	
	for i=1, 4 do
		backgroundOverlays[i] = {}
		for num=1, 4 do
			backgroundOverlays[i][num] = surface.GetTextureID( "ascension/hud/map_"..num.."_"..GetTeamColour(i) )		
		end
	end

end
hook.Add( "Initialize", "AscHUDInitialize", AscensionHUDInitialize )

function GetTeamColour( teamid )

	if (teamid == 1) then
		return "orange"
	elseif (teamid == 2) then		
		return "green"
	elseif (teamid == 3) then
		return "red"
	elseif (teamid == 4) then
		return "blue"
	end

end

function GetTeamPos( teamid )

	if (teamid == 1) then
		return orangePos
	elseif (teamid == 2) then		
		return greenPos
	elseif (teamid == 3) then
		return redPos
	elseif (teamid == 4) then
		return bluePos
	end

end

function AscensionDraw()

	/*---------------------------------------------------------------------------
		This draws the team progress hud
	*/---------------------------------------------------------------------------

	if ( !inOverview and LocalPlayer():Alive() and LocalPlayer():Team() != TEAM_UNASSIGNED and LocalPlayer():Team() != TEAM_SPECTATOR ) then
	
		surface.SetDrawColor( 255, 255, 255, 255 )
		
		surface.SetTexture( pointerBackground )
		surface.DrawTexturedRect( pointerXOffset - (backgroundWidth*0.5), ScrH() / 2 - (backgroundHeight*0.5), backgroundWidth, backgroundHeight )
		
		local teamid = LocalPlayer():Team()
		local pos = GetTeamPos( teamid )
		if (pos >= 128) then
			surface.SetTexture( backgroundOverlays[teamid][1] )
			surface.DrawTexturedRect( pointerXOffset - 8, ScrH() / 2 + 190, 16, 32 )
			
			surface.SetTexture( backgroundOverlays[teamid][2] )
			surface.DrawTexturedRect( pointerXOffset - 32, ScrH() / 2 + 62, 64, 128 )
		end
			
		if (pos >= 256) then
			surface.SetTexture( backgroundOverlays[teamid][2] )
			surface.DrawTexturedRect( pointerXOffset - 32, ScrH() / 2 - 66, 64, 128 )
		end
			
		if (pos >= 384) then
			surface.SetTexture( backgroundOverlays[teamid][3] )
			surface.DrawTexturedRect( pointerXOffset - 8, ScrH() / 2 - 82, 16, 16 )
			
			surface.SetTexture( backgroundOverlays[teamid][4] )
			surface.DrawTexturedRect( pointerXOffset - 64, ScrH() / 2 - 210, 128, 128 )
		end
		
		if ( GetNumPlyOnTeam(TEAM_ORANGE) > 0 ) then
			surface.SetTexture( pointerOrange )
			surface.DrawTexturedRect( pointerXOffset- pointerSize, ScrH() / 2 - (pointerSize*0.5) + 208 - orangePos, pointerSize, pointerSize )
		end
		
		if ( GetNumPlyOnTeam(TEAM_GREEN) > 0 ) then
			surface.SetTexture( pointerGreen )
			surface.DrawTexturedRect( pointerXOffset - pointerSize, ScrH() / 2 - (pointerSize*0.5) + 208 - greenPos, pointerSize, pointerSize )
		end
		
		if ( GetNumPlyOnTeam(TEAM_RED) > 0 ) then
			surface.SetTexture( pointerRed )
			surface.DrawTexturedRect( pointerXOffset, ScrH() / 2 - (pointerSize*0.5) + 208 - redPos, pointerSize, pointerSize )
		end
		
		if ( GetNumPlyOnTeam(TEAM_BLUE) > 0 ) then
			surface.SetTexture( pointerBlue )
			surface.DrawTexturedRect( pointerXOffset, ScrH() / 2 - (pointerSize*0.5) + 208 - bluePos, pointerSize, pointerSize )
		end
		
		
		local padding = 160
		local width = 128
		local height = 80
		
		surface.SetDrawColor( 255, 255, 255, 255 )
		
		surface.SetTexture( hudOne )
		surface.DrawTexturedRect( ScrW()/2-padding-(width/2), ScrH() - height, 128, 64 )

		surface.SetTexture( hudTwo )
		surface.DrawTexturedRect( ScrW()/2-(width/2), ScrH() - height, 128, 64 )

		surface.SetTexture( hudThree )
		surface.DrawTexturedRect( ScrW()/2+padding-(width/2), ScrH() - height, 128, 64 )
		
		
	end
	
	/*---------------------------------------------------------------------------
		This draws names above all of the players' heads.
	*/---------------------------------------------------------------------------
	
	for k, ply in pairs( player.GetAll() ) do
	
		if ( ply:Alive() and ply != LocalPlayer() and ply:Team() != TEAM_SPECTATOR and ply:Team() != TEAM_UNASSIGNED ) then
			
			local ScrPos = ( ply:GetPos() + Vector(0,0,112) ):ToScreen()
			
			draw.RoundedBox( 1, ScrPos.x - 20, ScrPos.y - 5, 40, 12, Color( 70, 125, 125, 150 ) )
			draw.SimpleTextOutlined( ply:Name(), "ChatFont1", ScrPos.x, ScrPos.y, Color(255,255,255,255), 1, 1, 1, Color(100,100,100,10) )
			
			surface.SetDrawColor( Color( 70, 125, 125, 150 ) )
			for i=-2, 2 do
				surface.DrawLine( ScrPos.x+i, ScrPos.y+7, ScrPos.x, ScrPos.y+12 );
			end
			
		end
		
	end
	
	
	/*---------------------------------------------------------------------------
		Changes the colour of a prop you're looking at.
	*/---------------------------------------------------------------------------
	
	local ply = LocalPlayer()
	if ( ply:Alive() and ply:Team() != TEAM_SPECTATOR and ply:Team() != TEAM_UNASSIGNED ) then
		
		local tr = ply:GetEyeTraceNoCursor()
		if ( ply.HoldingItem == nil ) then
			if ( tr.Entity:IsValid() and tr.Entity:GetClass() == "ascension_prop" ) then
				if (tr.Entity:GetOwner() == NULL and tr.Entity:GetPos():Distance( ply:GetPos() + Vector(0,0,64) ) < 100 ) then
					//tr.Entity:SetColor( 0,0,255,255 )
					local colour = tr.Entity:GetColour()
					tr.Entity:SetColor( colour.r, colour.g, colour.b, 255 )
				else
					tr.Entity:SetColor( 255,255,255,255 )
				end
			end
		end
		
	end
	
	if ( startMessage ) then
		
		if ( startTime+4 > CurTime() ) then
			local difference = startTime - CurTime()
			local sizeX = 128
			local sizeY = 128
			local minsizeX = 64
			local minsizeY = 64
			local startalpha = 255
			local endalpha = 100
			local textureSizeX = 0
			local textureSizeY = 0 
			local fadeMultiplier = 5
			
			if ( startTime+1 > CurTime() ) then
				textureSizeX = minsizeX+(sizeX-minsizeX)*(difference+1) 
				textureSizeY = minsizeY+(sizeY-minsizeY)*(difference+1) 
				surface.SetTexture( countdownThree )
				
				surface.SetDrawColor( 255, 255, 255, endalpha+(startalpha-endalpha)*(difference+1) )	
				surface.DrawTexturedRect( ScrW()/2-textureSizeX/2, ScrH()/2-textureSizeY/2, textureSizeX, textureSizeY )
				surface.SetDrawColor( 255, 255, 255, 40*(difference+1) )	
				surface.DrawTexturedRect( ScrW()/2-(textureSizeX*fadeMultiplier)/2, ScrH()/2-(textureSizeY*fadeMultiplier)/2, (textureSizeX*fadeMultiplier), (textureSizeY*fadeMultiplier) )
			elseif ( startTime+2 > CurTime() ) then
				textureSizeX = minsizeX+(sizeX-minsizeX)*(difference+2) 
				textureSizeY = minsizeY+(sizeY-minsizeY)*(difference+2) 
				surface.SetTexture( countdownTwo )
				
				surface.SetDrawColor( 255, 255, 255, endalpha+(startalpha-endalpha)*(difference+2) )			
				surface.DrawTexturedRect( ScrW()/2-textureSizeX/2, ScrH()/2-textureSizeY/2, textureSizeX, textureSizeY )
				surface.SetDrawColor( 255, 255, 255, 40*(difference+2) )	
				surface.DrawTexturedRect( ScrW()/2-(textureSizeX*fadeMultiplier)/2, ScrH()/2-(textureSizeY*fadeMultiplier)/2, (textureSizeX*fadeMultiplier), (textureSizeY*fadeMultiplier) )
			elseif ( startTime+3 > CurTime() ) then
				minsizeX, minsizeY = 32, 64
				sizeX, sizeY = 64, 128
				textureSizeX = minsizeX+(sizeX-minsizeX)*(difference+3) 
				textureSizeY = minsizeY+(sizeY-minsizeY)*(difference+3) 
				surface.SetTexture( countdownOne )
				
				surface.SetDrawColor( 255, 255, 255, endalpha+(startalpha-endalpha)*(difference+3) )
				surface.DrawTexturedRect( ScrW()/2-textureSizeX/2, ScrH()/2-textureSizeY/2, textureSizeX, textureSizeY )
				surface.SetDrawColor( 255, 255, 255, 40*(difference+3) )	
				surface.DrawTexturedRect( ScrW()/2-(textureSizeX*fadeMultiplier)/2, ScrH()/2-(textureSizeY*fadeMultiplier)/2, (textureSizeX*fadeMultiplier), (textureSizeY*fadeMultiplier) )
			elseif ( startTime+4 > CurTime() ) then	
				minsizeX, minsizeY = 128, 64
				sizeX, sizeY = 256, 128
				textureSizeX = minsizeX+(sizeX-minsizeX)*(difference+4) 
				textureSizeY = minsizeY+(sizeY-minsizeY)*(difference+4) 
				surface.SetTexture( countdownGo )
				
				surface.SetDrawColor( 255, 255, 255, endalpha+(startalpha-endalpha)*(difference+4) )
				surface.DrawTexturedRect( ScrW()/2-textureSizeX/2, ScrH()/2-textureSizeY/2, textureSizeX, textureSizeY )
				surface.SetDrawColor( 255, 255, 255, 40*(difference+4) )	
				surface.DrawTexturedRect( ScrW()/2-(textureSizeX*fadeMultiplier)/2, ScrH()/2-(textureSizeY*fadeMultiplier)/2, (textureSizeX*fadeMultiplier), (textureSizeY*fadeMultiplier) )
			end
		
		else
			startMessage = false
			startTime = 0
		end
		
	end

end 
hook.Add( "HUDPaint", "AscensionHUD", AscensionDraw )

function startGoMessage( um )

	startMessage = true
	startTime = CurTime()

end
usermessage.Hook( "startGoMessage", startGoMessage )

function UpdatePointers( um )

	orangePos = um:ReadLong()
	greenPos = um:ReadLong()
	redPos = um:ReadLong()
	bluePos = um:ReadLong()

end
usermessage.Hook( "UpdatePointers", UpdatePointers )

function UpdatePlayerIsHolding( um )

	local ply = um:ReadEntity()
	local ent = um:ReadEntity()
	
	ply.HoldingItem = ent

end
usermessage.Hook( "PlyIsHolding", UpdatePlayerIsHolding )

function UpdatePlayerIsHolding( um )

	local ply = um:ReadEntity()
	local ent = um:ReadEntity()
	
	ply.HoldingItem = ent

end
usermessage.Hook( "PlyIsHolding", UpdatePlayerIsHolding )

function UpdateHUD_CheckpointNotify( um )

	if ( IsValid( ascensionHud ) ) then ascensionHud:Remove() end		
    ascensionHud = vgui.Create( "DHudLayout" )
	checkpointThink = CurTime() + 5
	
	local Bar = vgui.Create( "DHudBar" )
    ascensionHud:AddItem( Bar, nil, 5 )

	local teamID = um:ReadShort()
	local Checkpoint = um:ReadShort()
	
	if ( type( teamID ) == "number" ) && ( team.GetAllTeams()[ teamID ] ) then
	
		local TeamName = team.GetName( teamID )

		if ( TeamName ) then 
		
			local teamText = vgui.Create( "DHudUpdater" )
				teamText:SizeToContents()
				teamText:SetValueFunction( function()
					return TeamName
				end )
				teamText:SetColorFunction( function()
					return team.GetColor( teamID )
				end )
				
			local reachedText = vgui.Create( "DHudUpdater" )
				reachedText:SizeToContents()
				reachedText:SetValueFunction( function()
					return " reached the "..GetCheckpointNumber(Checkpoint).." checkpoint!"
				end )

			Bar:AddItem( teamText )
			Bar:AddItem( reachedText )
		
		end

	end

end
usermessage.Hook( "CheckpointNotify", UpdateHUD_CheckpointNotify )



function GM:UpdateHUD_RoundResult( RoundResult, Alive ) 

    local txt = GetGlobalString( "RRText" )
	local Bar = vgui.Create( "DHudBar" )
	GAMEMODE:AddHUDItem( Bar, 5 )

	
	if ( type( RoundResult ) == "number" and RoundResult == TEAM_BLUE or RoundResult == TEAM_GREEN
		or RoundResult == TEAM_RED or RoundResult == TEAM_ORANGE ) then
		local teamText = vgui.Create( "DHudUpdater" );
			teamText:SizeToContents()
			teamText:SetValueFunction( function()
				return team.GetName( RoundResult )
			end )
			teamText:SetColorFunction( function()
				return team.GetColor( RoundResult )
			end )
	
		Bar:AddItem( teamText )

		local winsText = vgui.Create( "DHudUpdater" );
			winsText:SizeToContents()
			winsText:SetValueFunction( function()
				return "wins!"
			end )
			
		Bar:AddItem( winsText )
	else
		local drawText = vgui.Create( "DHudUpdater" );
			drawText:SizeToContents()
			drawText:SetValueFunction( function()
				return "Draw - nobody wins!"
			end )
			
		Bar:AddItem( drawText )
	end
	
	local timerBar = vgui.Create( "DHudBar" )
	GAMEMODE:AddHUDItem( timerBar, 8 )

	local timerText = vgui.Create( "DHudUpdater" );
	timerText:SizeToContents()
	timerText:SetValueFunction( function()
		local number = math.Round(timeToRestart-CurTime())
		if ( number > 0 ) then
			return "Round will restart in " .. math.Round(timeToRestart-CurTime())
		else
			return "Round will restart in 0"
		end
	end )

	timerBar:AddItem( timerText )

end

function UpdateHUD_TeamReachedTopNotify( um )
	
	if ( IsValid( teamCompleteNotify ) ) then teamCompleteNotify:Remove() end				
    teamCompleteNotify = vgui.Create( "DHudLayout" )
	teamCompleteThink = CurTime() + 5
	
	local Bar = vgui.Create( "DHudBar" )
    teamCompleteNotify:AddItem( Bar, nil, 5 )

	local teamID = um:ReadShort()
	local position = um:ReadShort()
	
	if ( type( teamID ) == "number" ) and ( team.GetAllTeams()[ teamID ] and type( position ) == "number" ) then
	
		local txt = nil
		local TeamName = team.GetName( teamID )
		
		if ( TeamName ) then
		
			local teamText = vgui.Create( "DHudUpdater" )
				teamText:SizeToContents()
				teamText:SetValueFunction( function()
					return TeamName
				end )
				teamText:SetColorFunction( function()
					return team.GetColor( teamID )
				end )
					
			local notifyText = vgui.Create( "DHudUpdater" )
				notifyText:SizeToContents()
				notifyText:SetValueFunction( function()
					return "came " .. GetRank(position) .. "!"
				end )

			Bar:AddItem( teamText )
			Bar:AddItem( notifyText )
		
		end

	end

end
usermessage.Hook( "TeamReachedTopNotify", UpdateHUD_TeamReachedTopNotify )

function StartRestartTimer( um )

	timeToRestart = um:ReadLong()

end
usermessage.Hook( "StartRestartTimer", StartRestartTimer )

function GetCheckpointNumber( checkpoint )

	if (checkpoint == 2) then
		return "1st"
	else
		return "2nd"
	end

end

function UpdateHUD_CheckpointThink()

	if ( checkpointThink < CurTime() and IsValid(ascensionHud) or !GetGlobalBool( "InRound" ) and IsValid(ascensionHud) ) then
		ascensionHud:Remove()
	end

end
hook.Add( "Think", "CheckpointThink", UpdateHUD_CheckpointThink )

function UpdateHUD_TeamReachedTopThink()

	if ( teamCompleteThink < CurTime() and IsValid(teamCompleteNotify) ) then
		teamCompleteNotify:Remove()
	end

end
hook.Add( "Think", "TeamReachedTopThink", UpdateHUD_TeamReachedTopThink )

function GetRank( num )

	if (num == 2) then
		return "2nd"
	elseif (num == 3) then
		return "3rd"
	elseif (num == 4) then
		return "4th"
	end

end

function GetNumPlyOnTeam( teamID )

	local num = 0
	for k, v in pairs( team.GetPlayers( teamID ) )  do
		if ( v:IsValid() and v:Alive() ) then
			num = num + 1
		end
	end
	
	return num

end

function GM:UpdateHUD_Alive( InRound )
 
    if ( GAMEMODE.RoundBased || GAMEMODE.TeamBased ) then
    
		local Bar = vgui.Create( "DHudBar" )
        GAMEMODE:AddHUDItem( Bar, 8 )
 
        if ( GAMEMODE.RoundBased ) then

        local RoundTimer = vgui.Create( "DHudCountdown" )
        RoundTimer:SizeToContents()
        RoundTimer:SetValueFunction( function()
		if ( GetGlobalFloat( "RoundStartTime", 0 ) > CurTime() ) then return GetGlobalFloat( "RoundStartTime", 0 )  end
        return GetGlobalFloat( "RoundEndTime" ) end )
        RoundTimer:SetLabel( "TIME" )
		Bar:AddItem( RoundTimer )

        end
    end

end


function GM:CreateScoreboard( ScoreBoard )

    // This makes it so that it's behind chat & hides when you're in the menu
    // Disable this if you want to be able to click on stuff on your scoreboard
    ScoreBoard:ParentToHUD()
	
    ScoreBoard:SetRowHeight( 32 )

    ScoreBoard:SetAsBullshitTeam( TEAM_SPECTATOR )
    ScoreBoard:SetAsBullshitTeam( TEAM_CONNECTING )
    ScoreBoard:SetShowScoreboardHeaders( GAMEMODE.TeamBased )
    
    if ( GAMEMODE.TeamBased ) then
        ScoreBoard:SetAsBullshitTeam( TEAM_UNASSIGNED )
        ScoreBoard:SetHorizontal( true )       
    end

    ScoreBoard:SetSkin( GAMEMODE.HudSkin )

    self:AddScoreboardAvatar( ScoreBoard )          // 1
    self:AddScoreboardWantsChange( ScoreBoard )     // 2
    self:AddScoreboardName( ScoreBoard )            // 3
    //    self:AddScoreboardKills( ScoreBoard )           // 4
    //   self:AddScoreboardDeaths( ScoreBoard )          // 5
    self:AddScoreboardPing( ScoreBoard )            // 6

    // Here we sort by these columns (and descending), in this order. You can define up to 4
    ScoreBoard:SetSortColumns( { 4, true, 5, false, 3, false } )

end

/*-------------------------------------------------
Chat Bubbles
--------------------------------------------------*/
surface.CreateFont("Arial", 12, 500, true, false, "ChatFont2");
surface.CreateFont("Arial", 15, 500, true, false, "ChatFont1");
Messages={};
for i=1,40 do Messages[i]=0 end;

function chatTextHook( ply, text, teamchat, alive )

	if ( alive ) then return end

	local Found = false;
	if (string.Left(text,1)=="#") then 
		Found = true; // Hide the message
	end
	
	// If not hidden
	if (!Found) then
		ChatMessage(text, ply:EntIndex());
	end

end
hook.Add( "OnPlayerChat", "ChatTextHook", chatTextHook ); 

function DrawBubbles()
	for k,v in pairs(Messages) do
		if (v!=0 && k<20) then
			local ply = player.GetByID(Messages[k+20]);
			if (ply:IsValid() and ply:Alive() and ply:Team() != TEAM_SPECTATOR and ply:Team() != TEAM_UNASSIGNED) then
				surface.SetFont( "ScoreboardText" );
				local pos = (ply:GetPos()+Vector(0,0,120)):ToScreen();
				local W, H = surface.GetTextSize(v);
				local NameW, NameH = surface.GetTextSize(ply:Nick().." says:");
				local BorderSize = 16;
				local OutlineSize = 4;
				
				if (string.len(v)<=2) then
					W=W+10;
				end
				if (W<(NameW*0.75)) then
					W = (NameW*0.75);
				end
				
				if (W>0&&H>0) then
					draw.RoundedBox(4, (pos.x-(W*0.5)-(BorderSize*0.5)-(OutlineSize*0.5)), pos.y-(BorderSize*0.5)-(OutlineSize*0.5)-(NameH*0.75), W+BorderSize+OutlineSize, H+BorderSize+OutlineSize+(NameH*0.75), Color(0,0,0,255)) 
					surface.SetDrawColor(255,255,255,255);
					for i=-1, 20 do
						surface.DrawLine( pos.x-9+i, pos.y+18, pos.x, pos.y+29 );
					end
					surface.SetDrawColor(0,0,0,255);
					for i=1, (OutlineSize*0.75) do
						surface.DrawLine( pos.x-(9+i), pos.y+20, pos.x, pos.y+29+i );
						surface.DrawLine( pos.x+(9+i), pos.y+20, pos.x, pos.y+29+i );
					end
					
					draw.RoundedBox(4, (pos.x-(W*0.5)-(BorderSize*0.5)), pos.y-(BorderSize*0.5)-(NameH*0.75), W+BorderSize, H+BorderSize+(NameH*0.75), Color(255,255,255,255)) 
					draw.SimpleText(ply:Nick().." says:", "ChatFont2", pos.x, pos.y-(NameH*0.75), Color(0,0,0,255), 1, 0);
					draw.SimpleText(v, "ChatFont1", pos.x, pos.y, Color(0,0,0,255), 1, 0);
				end
			end
		end
	end
end
hook.Add("HUDPaint", "DrawBubblesPaint", DrawBubbles)

function ChatMessage(msg, ply)
	for k,v in SortedPairs(Messages) do
		if (k>20) then
			if (v==ply) then
				Messages[k-20] = msg;
				return true;
			end
		end
	end
	for k,v in SortedPairs(Messages) do
		if (v==0 && k<20) then
			Messages[k+20]=ply;
			Messages[k] = msg;
			local Length = string.len(msg);
			timer.Simple(6+(Length*0.05), function()
				Messages[k]=0;
				Messages[k+20]=0;
			end);
			
			break; // We've found a slot for the message, no need to continue
		end
	end
end

local Help = nil 
function GM:ShowHelp()

	if ( !IsValid( Help ) ) then
	
		Help = vgui.CreateFromTable( vgui_Splash )
		Help:SetHeaderText( GAMEMODE.Name or "Untitled Gamemode" )
		Help:SetHoverText( GAMEMODE.Help or "No Help Avaliable" );
		
		Help.lblFooterText.Think = function( panel ) 
										local tl = GAMEMODE:GetGameTimeLeft()
										if ( tl == -1 ) then return end
										if( GetGlobalBool( "IsEndOfGame", false ) ) then panel:SetText( "Game has ended..." ) return end
										if( GAMEMODE.RoundBased && CurTime() > GAMEMODE:GetTimeLimit() ) then panel:SetText( "Game will end after this round" ) return end
										
										panel:SetText( "Time Left: " .. string.ToMinutesSeconds( tl ) ) 
									end

		if ( GetConVarNumber( "fretta_voting" ) != 0 ) then
			local btn = Help:AddSelectButton( "Vote For Change", function() RunConsoleCommand( "voteforchange" ) end )
			btn.m_colBackground = Color( 255, 200, 100 )
			btn:SetDisabled( LocalPlayer():GetNWBool( "WantsVote" ) ) 
		end
		
		if ( GAMEMODE.TeamBased ) then
			local btn = Help:AddSelectButton( "Change Team", function() GAMEMODE:ShowTeam() end )
			btn.m_colBackground = Color( 120, 255, 100 )
		end
		
		if ( !GAMEMODE.TeamBased && GAMEMODE.AllowSpectating ) then
		
			if ( LocalPlayer():Team() == TEAM_SPECTATOR ) then
			
				local btn = Help:AddSelectButton( "Join Game", function() RunConsoleCommand( "changeteam", TEAM_UNASSIGNED ) end )
				btn.m_colBackground = Color( 120, 255, 100 )
			
			else
		
				local btn = Help:AddSelectButton( "Spectate", function() RunConsoleCommand( "changeteam", TEAM_SPECTATOR ) end )
				btn.m_colBackground = Color( 200, 200, 200 )
				
			end
		end
		
		if ( IsValid( LocalPlayer() ) ) then
		
			local TeamID = LocalPlayer():Team()
			local Classes = team.GetClass( TeamID )
			if ( Classes && #Classes > 1 ) then
				local btn = Help:AddSelectButton( "Change Class", function() GAMEMODE:ShowClassChooser( LocalPlayer():Team() ) end )
				btn.m_colBackground = Color( 120, 255, 100 )
			end
		
		end
				
		Help:AddCancelButton()
		
		if ( GAMEMODE.SelectModel ) then
		
			local function CreateModelPanel()
							
				local pnl = vgui.Create( "DGrid" )
			
				pnl:SetCols( 6 )
				pnl:SetColWide( 66 )
				pnl:SetRowHeight( 66 )
			
				for name, model in pairs( list.Get( "PlayerOptionsModel" ) ) do
					
					local icon = vgui.Create( "SpawnIcon" )
					icon.DoClick = function() surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand( "cl_playermodel", name ) end
					icon.PaintOver = function() if ( GetConVarString( "cl_playermodel" ) == name ) then surface.SetDrawColor( Color( 255, 210 + math.sin(RealTime()*10)*40, 0 ) ) surface.DrawOutlinedRect( 4, 4, icon:GetWide()-8, icon:GetTall()-8 ) surface.DrawOutlinedRect( 3, 3, icon:GetWide()-6, icon:GetTall()-6 ) end end
					icon:SetModel( model )
					icon:SetSize( 64, 64 )
					icon:SetTooltip( name )
						
					pnl:AddItem( icon )
					
				end
				
				return pnl
				
			end
			
			Help:AddPanelButton( "gui/silkicons/user", "Choose Player Model", CreateModelPanel )
		
		end
		
		if ( GAMEMODE.SelectColor ) then
		
			local function CreateColorPanel()
							
				local pnl = vgui.Create( "DGrid" )
			
				pnl:SetCols( 10 )
				pnl:SetColWide( 36 )
				pnl:SetRowHeight( 128 )
			
				for name, colr in pairs( list.Get( "PlayerColours" ) ) do
					
					local icon = vgui.Create( "DButton" )
					icon:SetText( "" )
					icon.DoClick = function() surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand( "cl_playercolor", name ) end
					icon.Paint = function() surface.SetDrawColor( colr ) icon:DrawFilledRect() end
					icon.PaintOver = function() if ( GetConVarString( "cl_playercolor" ) == name ) then surface.SetDrawColor( Color( 255, 210 + math.sin(RealTime()*10)*40, 0 ) ) surface.DrawOutlinedRect( 4, 4, icon:GetWide()-8, icon:GetTall()-8 ) surface.DrawOutlinedRect( 3, 3, icon:GetWide()-6, icon:GetTall()-6 ) end end
					icon:SetSize( 32, 128 )
					icon:SetTooltip( name )
						
					pnl:AddItem( icon )
					
				end
				
				return pnl
				
			end
			
			Help:AddPanelButton( "gui/silkicons/application_view_tile", "Choose Player Color", CreateColorPanel )
			
		end

	end
	
	Help:MakePopup()
	Help:NoFadeIn()
	
end

